import pandas as pd
import os
import xlwings as xw

file_path = os.path.join('../Files', '相关性分析.xlsx')
df = pd.read_excel(file_path, sheet_name='Data', index_col='代理商编号')
result = df.corr()
values = pd.DataFrame(data=result)
app = xw.App(visible=False, add_book=False)
workbook = app.books.open(file_path)
sheet_names = [j.name for j in workbook.sheets]  # 获取所有sheet name
if 'Result' not in sheet_names:
    print('not existed ')
    worksheet_new = workbook.sheets.add('Result')
    worksheet_new['A1'].clear()
    worksheet_new['A1'].options(index=False).value = values
    worksheet_new.autofit()
workbook.save()
workbook.close()
app.quit()
print(result)
